package Model;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author thuydao
 */
import java.sql.*;
import java.util.ArrayList;
public class M_Customer {
        public M_Customer(){}
        private String makh;
        private String tenkh;
        private String gioitinh;
        private String sdt;
        private String diachi;
        private String cmnd;
        private Connection conn;
        
        public M_Customer(String makh, String tenkh, String gioitinh, String sdt, String diachi, String cmnd){
            this.makh=makh;
            this.tenkh = tenkh;
            this.gioitinh = gioitinh;
            this.sdt = sdt;
            this.diachi = diachi;
            this.cmnd = cmnd;
        }
        
           /**
     * @return the makh
     */
    public String getMakh() {
        return makh;
    }

    /**
     * @param makh the makh to set
     */
    public void setMakh(String makh) {
        this.makh = makh;
    }
        /**
     * @return the tenkh
     */
    public String getTenkh() {
        return tenkh;
    }

    /**
     * @param tenkh the tenkh to set
     */
    public void setTenkh(String tenkh) {
        this.tenkh = tenkh;
    }

    /**
     * @return the gioitinh
     */
    public String getGioitinh() {
        return gioitinh;
    }

    /**
     * @param gioitinh the gioitinh to set
     */
    public void setGioitinh(String gioitinh) {
        this.gioitinh = gioitinh;
    }

    /**
     * @return the sdt
     */
    public String getSdt() {
        return sdt;
    }

    /**
     * @param sdt the sdt to set
     */
    public void setSdt(String sdt) {
        this.sdt = sdt;
    }

    /**
     * @return the diachi
     */
    public String getDiachi() {
        return diachi;
    }

    /**
     * @param diachi the diachi to set
     */
    public void setDiachi(String diachi) {
        this.diachi = diachi;
    }

    /**
     * @return the cmnd
     */
    public String getCmnd() {
        return cmnd;
    }

    /**
     * @param cmnd the cmnd to set
     */
    public void setCmnd(String cmnd) {
        this.cmnd = cmnd;
    }

    /**
     * @param conn the conn to set
     */
    public void setConn(Connection conn) {
        this.conn = conn;
    }
        
        public void connect(){
         try {
            String userName = "root"; // root là mặc định của MySQL.
            String passWord = "";
            String url = 
            "jdbc:mysql://localhost:3306/handmade?useUnicode=true&characterEncoding=UTF-8"; 
           
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            setConn(DriverManager.getConnection(url, userName, passWord));
            }
            catch (Exception e) {
                System.out.println("Diver sai");
            }
        
        }
    public boolean testAccount(String str_tendn) {
        
        ResultSet rs = null;
       
        boolean result = false;
        connect();
        try {
           
            String sql = "SELECT * FROM taikhoan WHERE TenDN = '"+str_tendn+"'";
            java.sql.Statement stmt = conn.createStatement();
            rs = stmt.executeQuery(sql);
            
        
            //boolean result = rs.next();
    
            //if (result) {
               // return true;
            //}
            
            String str=null;
            
            if(rs.next())
            {
                 str = rs.getString("TenDN");
            }
             if(str.compareTo(str_tendn)==0)
                result = true;
            stmt.close();
            rs.close();
            conn.close();
            
        } catch (Exception e) {
            e.printStackTrace();
            
        }
        return result;
    }
    
    // Tạo Mã khách hàng
    public String createMaKH (){
        connect();
        java.sql.Statement stmt = null;
        ResultSet rs = null;
        String MA = "";
        
       try {
            
            
            
            
            String sql = "SELECT * FROM khachhang";
         
            stmt = conn.createStatement();
            rs = stmt.executeQuery(sql);
            int dem = 1;
            while(rs.next()) { // Duyệt RecordSet
                dem++;
            } 
            String str = Integer.toString(dem);
            MA = "KH" + str;
            
            stmt.close();
            rs.close();
            conn.close();
            return MA;
            
        } catch (Exception e) {
            e.printStackTrace();
        }
       return MA;
    }
    
    // Thêm vào bảng khách hàng
    
    public boolean insertKH(String makh, String tenkh, String gioitinh, String cmnd, String sdt, String diachi){
            connect();
         
            
            
        try{
                
            
            String sql_insert_KH = "insert into khachhang values("; 
            sql_insert_KH += "'"+makh+"',"; 
            sql_insert_KH += "'"+tenkh+"',"; 
            sql_insert_KH += "'"+gioitinh+"',"; 
            sql_insert_KH += "'"+cmnd+"',"; 
            sql_insert_KH += "'"+sdt+"',";
            sql_insert_KH += "'"+diachi+"')";
           //String sql_insert_KH = "insert into KHACHHANG values( ?, ?, ?, ?, ?, ?)"; 

            //PreparedStatement stm = conn.prepareStatement(sql_insert_KH);
            //stm.setString(1, makh);
            //stm.setString(2, tenkh);
            //stm.setString(3, gioitinh);
            //stm.setString(4, cmnd);
            //stm.setString(5, sdt);
            //stm.setString(6, diachi);
              
              
            

            java.sql.Statement st = conn.createStatement();
           int val = st.executeUpdate(sql_insert_KH);
            //result = "Đã thêm "+String.valueOf(val)+" dòng thành công";
             // System.out.println("Đã thêm "+String.valueOf(val)+" dòng thành công");
           if(val>0)
               return true;
            }
            catch (SQLException s){
                System.out.println("SQL statement is not executed! <br>"+s.getMessage());
            //result = "SQL statement is not executed! <br>"+s.getMessage();
            }
       return false;
          
   }
    
    public boolean insertTK(String matk, String tendn, String matkhau){
            connect();
            ResultSet rs = null;
            boolean result = false;
           
          try{
            String sql_insert_TK = "insert into taikhoan values("; 
            sql_insert_TK += "'"+matk+"',"; 
            sql_insert_TK += "'"+tendn+"',"; 
            
         
            sql_insert_TK += "'"+matkhau+"')";



            Statement st = conn.createStatement();
            int val= st.executeUpdate(sql_insert_TK);
            result=true;
            }
            catch (SQLException s){
                System.out.println("SQL insert TAIKHOAN statement is not executed!"+ s.getMessage());
            }
          return result;
   }
    
    public boolean update_KH(){
            connect();
            
            
           
          try{
            String sql_edit_KH = "update KHACHHANG set TenKH=?, GioiTinh=?, SDT=?, CMND=?, DiaChi=? where MaKH=?"; 
            PreparedStatement stm = conn.prepareStatement(sql_edit_KH);
            stm.setString(1, tenkh);
            stm.setString(2, gioitinh);
            stm.setString(3, sdt);
            stm.setString(4, cmnd);
            stm.setString(5, diachi);
            stm.setString(6, makh);
           
            int val= stm.executeUpdate();
            stm.close();
            conn.close();
           if(val>0)
                return true;
        
            }
            catch (SQLException s){
                System.out.println("QL insert TAIKHOAN statement is not executed!"+ s.getMessage());
            }
          return false;
   }
    // Xóa khách hàng
     public boolean delete_KH(){
            connect();
            
            
           //DELETE FROM `danhmucsp` WHERE 1
          try{
            String sql_delete_TK = "Delete from KHACHHANG Where MaKH=?"; 
            
            PreparedStatement stm = conn.prepareStatement(sql_delete_TK);
            stm.setString(1, makh);
            
            int val= stm.executeUpdate();
            stm.close();
            conn.close();
            if(val>0)
                return true;
            
            
            }
            catch (SQLException s){
                System.out.println(s.getMessage());
            }
          return false;
   }
     
    // Xóa tài khoản
     
     public boolean delete_TK(){
            connect();
            
            
           //DELETE FROM `danhmucsp` WHERE 1
          try{
            String sql_delete_TK = "Delete from TAIKHOAN Where MaTK=?"; 
            
            PreparedStatement stm = conn.prepareStatement(sql_delete_TK);
            stm.setString(1, makh);
            
            int val= stm.executeUpdate();
            stm.close();
            conn.close();
            if(val>0)
                return true;
            
            
            }
            catch (SQLException s){
                System.out.println(s.getMessage());
            }
          return false;
   }
    
    public M_Customer[] show_KH(){
            connect();
            ResultSet rs=null;
            M_Customer[] result=null;
            try{
            String sql_show_KH = "select *from khachhang"; 
           
            Statement st = conn.createStatement();
            rs= st.executeQuery(sql_show_KH);
           
            ArrayList list = new ArrayList();
            while(rs.next()){
                String makh = rs.getString("MaKH");
                String tenkh = rs.getString("TenKH");
                String gioitinh = rs.getString("GioiTinh");
                String sdt = rs.getString("SDT");
                String diachi = rs.getString("DiaChi");
                String cmnd = rs.getString("CMND");
                M_Customer cus = new M_Customer(makh, tenkh, gioitinh, sdt, diachi, cmnd);
                list.add(cus);
                
                
            }
            result = new M_Customer[list.size()];
            list.toArray(result);
            
            rs.close();
            st.close();
            conn.close();
            return result;
            }
            catch (SQLException s){
                System.out.println(s.getMessage());
            }
            return null;
            
    }
    
    public M_Customer[] search_KH(String search){
            connect();
            ResultSet rs = null;
            M_Customer[] result=null;
            try{
            String sql_search_KH = "select *from khachhang where TenKH like '%" + search + "%' or GioiTinh like '%" + search + "%' or DiaChi like '%" + search + "%'"; 
           
            Statement st = conn.createStatement();
            rs= st.executeQuery(sql_search_KH);
           
            ArrayList list = new ArrayList();
            while(rs.next()){
                String makh = rs.getString("MaKH");
                String tenkh = rs.getString("TenKH");
                String gioitinh = rs.getString("GioiTinh");
                String sdt = rs.getString("SDT");
                String diachi = rs.getString("DiaChi");
                String cmnd = rs.getString("CMND");
                M_Customer cus = new M_Customer(makh, tenkh, gioitinh, sdt, diachi, cmnd);
                list.add(cus);  
                
                
            }
            result = new M_Customer[list.size()];
            list.toArray(result);
            
            rs.close();
            st.close();
            conn.close();
            return result;
            }
            catch (SQLException s){
                System.out.println(s.getMessage());
            }
            return null;
            
    }

    public M_Customer[] search_KH_TenDN(String strtendn){
           connect();
            ResultSet rs;
            M_Customer[] result;
            try{
            //select TenKH, GioiTinh, SDT, DiaChi, CMND from KHACHHANG, TAIKHOAN WHERE TAIKHOAN.MaTK=KHACHHANG.MaKH and TenDN ='l'
            String sql_search_KH = "Select * from KHACHHANG, TAIKHOAN WHERE TAIKHOAN.MaTK=KHACHHANG.MaKH and TenDN ='" + strtendn + "'";  
           
            java.sql.Statement st = conn.createStatement();
            rs= st.executeQuery(sql_search_KH);
           
            ArrayList list = new ArrayList();
            while(rs.next()){
                String str_makh = rs.getString("MaKH");
                String str_tenkh = rs.getString("TenKH");
                String str_gioitinh = rs.getString("GioiTinh");
                String str_sdt = rs.getString("SDT");
                String str_diachi = rs.getString("DiaChi");
                String str_cmnd = rs.getString("CMND");
                M_Customer cus = new M_Customer(str_makh, str_tenkh, str_gioitinh, str_sdt, str_diachi, str_cmnd);
                list.add(cus);
                
                
            }
            result = new M_Customer[list.size()];
            list.toArray(result);
            
            rs.close();
            st.close();
            conn.close();
            return result;
            }
            catch (SQLException s){
                System.out.println(s.getMessage());
            }
            return null;
            
    }
    
   

    
    
    
}
